package l.l.w.leetcode.RemoveDuplicatesFromSortedArray;

/**
 * Created by llw on 2017/11/3.
 */
public class RemoveDuplicatesFromSortedArray {
    public int removeDuplicates(int[] nums) {
        if(nums == null || nums.length ==0) return 0;
        if(nums.length ==1) return 1;
        int result = 1;
        int pre = nums[0];
        for(int i  =1; i < nums.length; ++i){
            if(nums[i] == pre){
                nums[i] = Integer.MIN_VALUE;
                continue;
            }else{
                result++;
            }
            pre = nums[i];
        }
        int j = 0;
        for(int i = 0; i < nums.length;++i){
            if(nums[i]!= Integer.MIN_VALUE){
                exchange(nums,i,j);
                j++;
            }
        }
        return result;
    }

    private void exchange(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public static void main(String[] args) {
        int[] a = {0,0,3};
        int result = new RemoveDuplicatesFromSortedArray().removeDuplicates(a);
        System.out.println(result);
    }
}
